1 시각화 따라 해 보기
아래 사이트 접속 후 Rstudio에서 그래프 하나씩 따라해보세요.
[한국 R 사용자회 – 챗GPT 데이터 시각화 (r2bit.com)] https://r2bit.com/bitSlide/chatgpt_viz_202406.html#/데이터-시각화
library(tidyverse)
library(plotly)
library(gapminder)
library(crosstalk)
library(leaflet)
library(flipbookr)
head(gapminder)
# A tibble: 6 × 6
country continent year lifeExp pop gdpPercap
<fct> <fct> <int> <dbl> <int> <dbl>
1 Afghanistan Asia 1952 28.8 8425333 779.
2 Afghanistan Asia 1957 30.3 9240934 821.
3 Afghanistan Asia 1962 32.0 10267083 853.
4 Afghanistan Asia 1967 34.0 11537966 836.
5 Afghanistan Asia 1972 36.1 13079460 740.
6 Afghanistan Asia 1977 38.4 14880372 786.
1 ggplot으로 연도에 따른 기대 수명을 나라별로 그리기
2 ggplot을 ggplotly에 넣어서 interactive 그래프 만들기
4 Highlight 기능 넣기 (검색박스 만들기)
6 연결뷰 기능 (여러개 그래프 중 하나를 줌인 하면 다른 것도 줌인 되는 것)
7 대륙별로 1인당 gdp (gdpPercap) 과 기대수명(lifeExp) 를 연도별로 그래프 그리기
(ggplotly 사용)
8 대륙별로 1인당 gdp (gdpPercap) 과 기대수명(lifeExp) 를 연도별로 그래프 그리기(애니메이션)
(geom_point(aes(frame = year) 사용하기)
9 그래프 따라하기(1)
theme(legend.position = “top”, axis.text.x = element_text(angle = 90, hjust = 1)) 사용하기
10 그래프 따라하기(2)
facet_wrap(~country, scale=“free”) 사용하기
12 애니메이션 구현하기
따라서 작성해보기
gif 파일로 저장하기
library(gganimate)
life <- gapminder %>% filter( country %in% c("Korea, Rep.","Korea, Dem. Rep.", "China", "United States", "Japan")) %>%
ggplot(aes(x=year, y=lifeExp, group = country, col=country))+
geom_line(alpha=0.3, linewidth=1.5) +
geom_point(aes(frame = `year`), size=3.5) +
# scale_x_date(date_breaks="1 week", date_labels="%m-%d") +
# scale_y_continuous(labels=scales::percent) +
theme_bw(base_family="NanumGothic") +
labs(x="", y="기대수명", color="") +
theme(legend.position = "top",
axis.text.x = element_text(angle = 90, hjust = 1),
axis.text=element_text(size=16, color="black"),
legend.text=element_text(size=18),
plot.title = element_text(size=22))
gganimate(life)
gganimate(life, "기대수명.gif", ani.width = 640, ani.height = 480)
13 인터랙티브 그래프
- 펭귄 종별 몸무게
- 펭귄 지느러미 길이와 몸무게
14 ggiraph 패키지 연습해보기
library(ggiraph)
dat <- gapminder::gapminder |>
janitor::clean_names() |>
mutate(
# Reformat continent as a character instead of as a factor
# (will be important later)
id = levels(continent)[as.numeric(continent)],
continent = forcats::fct_reorder(continent, life_exp)
)
color_palette <- thematic::okabe_ito(5)
names(color_palette) <- unique(dat$continent)
base_size <- 18
mean_life_exps <- dat |>
group_by(continent, year, id) |>
summarise(mean_life_exp = mean(life_exp)) |>
ungroup()
line_chart <- mean_life_exps |>
ggplot(aes(x = year, y = mean_life_exp, col = continent)) +
geom_line(linewidth = 2.5) +
geom_point(size = 4) +
theme_minimal(base_size = base_size) +
labs(
x = element_blank(),
y = 'Life expectancy (in years)',
title = 'Life expectancy over time'
) +
theme(
text = element_text(
color = 'grey20'
),
legend.position = 'none',
panel.grid.minor = element_blank(),
plot.title.position = 'plot'
) +
scale_color_manual(values = color_palette)
line_chart
library(ggiraph)
line_chart <- mean_life_exps |>
ggplot(aes(x = year, y = mean_life_exp, col = continent, data_id = id)) +
geom_line_interactive(linewidth = 2.5) +
geom_point_interactive(size = 4) +
theme_minimal(base_size = base_size) +
labs(
x = element_blank(),
y = 'Life expectancy (in years)',
title = 'Life expectancy over time'
) +
theme(
text = element_text(
color = 'grey20'
),
legend.position = 'none',
panel.grid.minor = element_blank(),
plot.title.position = 'plot'
) +
scale_color_manual(values = color_palette)
girafe(ggobj = line_chart)
girafe(
ggobj = line_chart,
options = list(
opts_hover(css = ''), ## CSS code of line we're hovering over
opts_hover_inv(css = "opacity:0.1;"), ## CSS code of all other lines
opts_sizing(rescale = FALSE) ## Fixes sizes to dimensions below
),
height_svg = 6,
width_svg = 9
)